package com.woyowodaows.fragment.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.woyowodaows.R;
import com.woyowodaows.activity.mine.AccountSecurityActivity;
import com.woyowodaows.activity.mine.AddressManageActivity;
import com.woyowodaows.activity.mine.DetailedAddressActivity;
import com.woyowodaows.activity.mine.NewAddServiceActivity;
import com.woyowodaows.activity.mine.NewCentreActivity;
import com.woyowodaows.activity.mine.PersonalCenteActivity;
import com.woyowodaows.activity.mine.ServiceManagementActivity;
import com.woyowodaows.activity.mine.SettingAboutActivity;
import com.woyowodaows.activity.mine.SettingActivity;
import com.woyowodaows.base.BaseFragment;
import com.woyowodaows.bean.mine.UserInfoBean;
import com.woyowodaows.bean.mine.UserinfoData;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.CacheUserInfoUtils;
import com.woyowodaows.utils.GlideImageLoader;
import com.woyowodaows.utils.JpushTagData;
import com.woyowodaows.utils.LoginJudge;
import com.woyowodaows.utils.SPUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.jpush.android.api.JPushInterface;

public class MineFragment extends BaseFragment {

    @BindView(R.id.iv_mine_head)
    ImageView ivMineHead;
    @BindView(R.id.tv_mine_name)
    TextView tvMineName;
    @BindView(R.id.tv_mine_time)
    TextView tvMineTime;
    @BindView(R.id.bt_mine_history_indent)
    Button btMineHistoryIndent;
    @BindView(R.id.bt_mine_repairs)
    Button btMineRepairs;
    @BindView(R.id.tv_my_mine_address)
    TextView tvMyMineAddress;
    @BindView(R.id.tv_my_mine_messges)
    TextView tvMyMineMessges;
    @BindView(R.id.tv_message_off)
    TextView tvMessageOff;
    @BindView(R.id.tv_my_mine_questions)
    TextView tvMyMineQuestions;
    @BindView(R.id.sw_refresh)
    SwipeRefreshLayout swRefresh;


    private UserInfoBean userInfoBean;


    @Override
    protected void initView() {
        ButterKnife.bind(this, layoutView);
        swRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getPersonInfoData();
            }
        });


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.bt_mine_history_indent,
            R.id.bt_mine_repairs, R.id.tv_my_mine_address,
            R.id.rl_my_mine_messges,
            R.id.tv_my_mine_questions,
            R.id.iv_mine_set,
            R.id.iv_mine_head})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_mine_history_indent:
                LoginJudge.isLogin(mContext, ServiceManagementActivity.class, null);
                break;
            case R.id.bt_mine_repairs:
                LoginJudge.isLogin(mContext, NewAddServiceActivity.class, null);
                break;
            case R.id.tv_my_mine_address:
                LoginJudge.isLogin(mContext, AddressManageActivity.class, null);
                break;
            case R.id.rl_my_mine_messges:
                LoginJudge.isLogin(mContext, NewCentreActivity.class, null);
                break;
            case R.id.tv_my_mine_questions://关于我们
                startActivity(new Intent(mContext,SettingAboutActivity.class));
                break;
            case R.id.iv_mine_set://账号安全
                startActivity(new Intent(mContext, AccountSecurityActivity.class));
                break;
            case R.id.iv_mine_head:
                //PersonalCenteActivity
                LoginJudge.isLogin(mContext, PersonalCenteActivity.class, null);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        boolean isLogin = (boolean) SPUtils.get(mContext, SPUtils.IS_LOGIN, false);
        if (!isLogin) {
//            CacheUserInfoUtils.setUserInfo(null);//清空数据库数据
            GlideImageLoader.displayHeadImage(mContext,"", R.mipmap.icon_header, ivMineHead);
            swRefresh.setEnabled(false);
        } else {
            swRefresh.setEnabled(true);
        }
        getPersonInfoData();
    }

    private void getPersonInfoData() {
        boolean isLogin = (boolean) SPUtils.get(mContext, SPUtils.IS_LOGIN, false);
        if (!isLogin) {
            tvMineName.setText("游客");
            tvMineTime.setText("暂无");
            btMineRepairs.setVisibility(View.VISIBLE);
            return;
        }else {
            setUserInfoBean();
        }
        Map<String, Object> mapParameter = new HashMap<>();

        HttpAsyncTask.getInstance().onPostParam(mContext, "", false, InterfaceUrl.USESERINFO, UserinfoData.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    UserinfoData bean = (UserinfoData) result;
                    if (HttpAsyncTask.SUCCESS == bean.getResult()) {
                        UserInfoBean oldUserInfo=CacheUserInfoUtils.getUserInfo();

                        UserInfoBean userInfoBean = bean.getData();
                        userInfoBean.setAvatar(bean.getFileUrl() + userInfoBean.getAvatar());

                        CacheUserInfoUtils.setUserInfo(userInfoBean);
                        if(oldUserInfo==null){
                            setUserInfoBean();
                            return;
                        }
                        if(!String.valueOf(oldUserInfo.getDistributionAreaIds()).equals(String.valueOf(userInfoBean.getDistributionAreaIds()))){
                            setUserInfoBean();
                        }
                    }
                }
                swRefresh.setRefreshing(false);
            }

            @Override
            public void onError(String message) {
                setUserInfoBean();
                swRefresh.setRefreshing(false);
            }
        });
    }

    private void setUserInfoBean() {
        userInfoBean = CacheUserInfoUtils.getUserInfo();
        if(userInfoBean!=null){
            tvMineName.setText(userInfoBean.getNickname());
            tvMineTime.setText(userInfoBean.getCreateTime());

            GlideImageLoader.displayHeadImage(mContext,userInfoBean.getAvatar(), R.mipmap.icon_header, ivMineHead);
            if(userInfoBean.getDistributionAreaIds()!=null && !"".equals(userInfoBean.getDistributionAreaIds())){
                btMineRepairs.setVisibility(View.GONE);
            }else {
                btMineRepairs.setVisibility(View.VISIBLE);
            }
            if(userInfoBean.getMsgs()>0){
                tvMessageOff.setBackgroundResource(R.mipmap.icon_news_number_on);
            }else {
                tvMessageOff.setBackgroundResource(R.mipmap.icon_news_number);
            }
            JPushInterface.setAlias(mContext,202,userInfoBean.getId());

            String distributionAreaIds=userInfoBean.getDistributionAreaIds();
            Set<String> tags = new HashSet<String>();
            if(distributionAreaIds!=null && !"".equals(distributionAreaIds)){
                String[] sourceStrArray = distributionAreaIds.split(",");
                for (int i = 0; i < sourceStrArray.length; i++) {
                    tags.add(sourceStrArray[i]);
                }
            }else{
                tags.add("_1");
            }
            JPushInterface.setTags(mContext,2,tags);

        }else {
            tvMineName.setText("游客");
            tvMineTime.setText("暂无");
            Set<String> tags = new HashSet<String>();
            tags.add("_1");
            JPushInterface.setTags(mContext,-1,tags);
        }
    }


}
